﻿package mortal.game.view.pack.giftPackOpen
{
    import com.mui.utils.*;
    import extend.language.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.copy.ZZXYBattle.*;
    import mortal.mvc.interfaces.*;

    public class GiftPackOpenWin extends BaseWindow
    {
        private var _cards:Array;
        private var _selectedCard:ZZXYBattleRewardCard;
        private var _toBeUseItem:ItemData;
        private var _lastOpenTime:int = -1;
        private var _waitTimerId:int = -1;
        private static var _instance:GiftPackOpenWin;

        public function GiftPackOpenWin(param1:ILayer = null)
        {
            super(param1);
            this.setSize(735, 512);
            this.title = Language.getString(35460);
            return;
        }// end function

        public function useItem(param1:ItemData) : void
        {
            this._toBeUseItem = param1;
            this.show();
            this.disposeTimer();
            this.resetAll();
            return;
        }// end function

        public function updateToResult(param1:ItemData) : void
        {
            var _loc_4:ItemListData = null;
            var _loc_7:int = 0;
            var _loc_8:ZZXYBattleCardData = null;
            var _loc_9:ZZXYBattleRewardCard = null;
            if (this._cards == null || this._toBeUseItem == null)
            {
                return;
            }
            var _loc_2:* = ItemsUtil.parseItemListData((this._toBeUseItem.itemInfo as ItemGiftBagInfo).itemList1);
            var _loc_3:* = _loc_2.length;
            var _loc_5:int = 0;
            while (_loc_5 < 100)
            {
                
                _loc_7 = int(Math.random() * _loc_3);
                _loc_4 = _loc_2[0];
                _loc_2[0] = _loc_2[_loc_7];
                _loc_2[_loc_7] = _loc_4;
                _loc_5++;
            }
            _loc_5 = 0;
            while (_loc_5 < _loc_3)
            {
                
                _loc_4 = _loc_2[_loc_5];
                if (_loc_4.code == param1.itemInfo.code || _loc_4.code == param1.itemInfo.codeUnbind)
                {
                    _loc_4 = _loc_2[this._selectedCard.index];
                    _loc_2[this._selectedCard.index] = _loc_2[_loc_5];
                    _loc_2[_loc_5] = _loc_4;
                    break;
                }
                _loc_5++;
            }
            var _loc_6:Array = [];
            _loc_5 = 0;
            while (_loc_5 < _loc_3)
            {
                
                _loc_4 = _loc_2[_loc_5];
                _loc_8 = new ZZXYBattleCardData();
                _loc_8.belongsToMe = _loc_5 == this._selectedCard.index;
                _loc_8.itemCode = _loc_4.code;
                _loc_8.num = _loc_4.num;
                _loc_6.push(_loc_8);
                _loc_5++;
            }
            _loc_5 = 0;
            while (_loc_5 < this._cards.length)
            {
                
                _loc_9 = this._cards[_loc_5];
                _loc_9.reset();
                _loc_9.updateItem(_loc_6[_loc_5] as ZZXYBattleCardData);
                _loc_5++;
            }
            this.setAllLocked(true);
            if (this._selectedCard != null)
            {
                this._selectedCard.startRolling();
                this._selectedCard.filters = [FilterConst.guideFilter];
            }
            this.disposeTimer();
            this.addTimer();
            return;
        }// end function

        private function disposeTimer() : void
        {
            if (this._waitTimerId > 0)
            {
                clearTimeout(this._waitTimerId);
                this._waitTimerId = -1;
            }
            return;
        }// end function

        private function addTimer() : void
        {
            this._waitTimerId = setTimeout(this.rollingOthersCards, 2500);
            return;
        }// end function

        private function rollingOthersCards() : void
        {
            var _loc_1:ZZXYBattleRewardCard = null;
            if (isDisposed || this._cards == null)
            {
                return;
            }
            for each (_loc_1 in this._cards)
            {
                
                if (this._selectedCard != null && _loc_1.index == this._selectedCard.index)
                {
                    continue;
                }
                _loc_1.openedFlyToPack = false;
                _loc_1.startRolling();
            }
            if (this._selectedCard != null)
            {
                this._selectedCard.filters = [FilterConst.guideFilter];
            }
            return;
        }// end function

        public function resetAll() : void
        {
            var _loc_2:ZZXYBattleRewardCard = null;
            if (this._cards == null)
            {
                return;
            }
            this.setAllLocked(false);
            var _loc_1:int = 0;
            while (_loc_1 < this._cards.length)
            {
                
                _loc_2 = this._cards[_loc_1];
                _loc_2.reset();
                _loc_2.openedFlyToPack = true;
                _loc_2.filters = [];
                _loc_1++;
            }
            return;
        }// end function

        private function setAllLocked(param1:Boolean) : void
        {
            var _loc_2:ZZXYBattleRewardCard = null;
            if (this._cards == null)
            {
                return;
            }
            for each (_loc_2 in this._cards)
            {
                
                if (_loc_2 != null)
                {
                    _loc_2.mouseChildren = !param1;
                    _loc_2.mouseEnabled = !param1;
                }
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            var _loc_2:ZZXYBattleRewardCard = null;
            super.createDisposedChildrenImpl();
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.GiftBagOpenWinBg, 6, 37, this));
            this._cards = [];
            var _loc_1:int = 0;
            while (_loc_1 < 6)
            {
                
                _loc_2 = UICompomentPool.getUICompoment(ZZXYBattleRewardCard) as ZZXYBattleRewardCard;
                _loc_2.openImg = ImagesConst.GiftBagOpen_OpenedCard;
                _loc_2.closeImg = ImagesConst.GiftBagOpen_ClosedCard;
                _loc_2.item.x = -30;
                _loc_2.clickOpenRightNow = false;
                _loc_2.openCallback = this.openCardHandler;
                _loc_2.x = 150 + _loc_1 % 3 * 151;
                _loc_2.y = 110 + int(_loc_1 / 3) * 170;
                _loc_2.index = _loc_1;
                this._cards.push(_loc_2);
                this.addChild(_loc_2);
                _loc_1++;
            }
            return;
        }// end function

        private function openCardHandler(param1:ZZXYBattleRewardCard) : void
        {
            var _loc_2:* = getTimer();
            if (_loc_2 - this._lastOpenTime <= 1000)
            {
                return;
            }
            this._lastOpenTime = _loc_2;
            if (this._selectedCard != null)
            {
                this._selectedCard.filters = [];
            }
            this._selectedCard = param1;
            if (this._toBeUseItem == null || this._toBeUseItem.serverData == null)
            {
                return;
            }
            GameProxy.packProxy.useItem(this._toBeUseItem.serverData.uid, this._toBeUseItem.itemInfo.code);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            DisplayUtil.delNotUse(this._cards, 0);
            this.disposeTimer();
            if (this._selectedCard != null)
            {
                this._selectedCard.filters = [];
            }
            this._selectedCard = null;
            return;
        }// end function

        public static function get instance() : GiftPackOpenWin
        {
            if (_instance == null)
            {
                _instance = new GiftPackOpenWin;
            }
            return _instance;
        }// end function

        public static function hasShowingInstance() : Boolean
        {
            if (_instance == null)
            {
                return false;
            }
            if (_instance.isHide)
            {
                return false;
            }
            return true;
        }// end function

    }
}
